//
//  FirstViewController.swift
//  WKWebViewTest
//
//  Created by 刘礼燕 on 2017/8/30.
//  Copyright © 2017年 LLyan. All rights reserved.
//

import UIKit
import SnapKit

class FirstViewController: UIViewController {

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        /*设置因导航栏遮挡view的问题*/
//        self.automaticallyAdjustsScrollViewInsets = false
        self.navigationController?.navigationBar.isTranslucent = false
        self.tabBarController?.tabBar.isTranslucent = false

        
        let label = UILabel()
        label.text = "走过红尘的纷扰，弹落灵魂沾染的尘埃，携一抹淡淡的情怀，迎着清馨的微风，坐在岁月的源头，看时光婆娑的舞步，让自己安静在时间的沙漏里"
        label.textColor = UIColor.red
//        label.backgroundColor = UIColor.green
        self.view.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.top.equalTo(10)
            make.width.equalTo(200)
//            make.height.equalTo(30)
        }
        label.layer.masksToBounds = true
        label.layer.cornerRadius = 9.5
        label.layer.borderWidth = 1.0
        label.layer.borderColor = UIColor.blue.cgColor
        //阴影偏移量，默认值0到3
//        label.layer.shadowOffset = CGSize(width: 0, height: -4)
        //阴影颜色
//        label.layer.shadowColor = UIColor.black.cgColor
        //阴影透明度
//        label.layer.shadowOpacity = 0.8
//        label.layer.shadowRadius = -3;
        label.numberOfLines = 0
        //设置换行方式
//        label.lineBreakMode = NSLineBreakMode(rawValue: 0)!
        label.font = UIFont.systemFont(ofSize: 15)
        label.textAlignment = NSTextAlignment.left
//        label.font = UIFont.preferredFont(forTextStyle: .body)
//        label.sizeToFit()
        
        let button = UIButton()
        button.setTitle("打印数据", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.backgroundColor = UIColor.blue
        button.addTarget(self, action: #selector(printDataClick(_ :)), for: UIControlEvents.touchUpInside)
        self.view.addSubview(button)
        button.snp.makeConstraints { (make) in
            make.top.equalTo(label.snp.bottom).offset(20)
            make.left.equalTo(20)
            make.width.equalTo(80)
            make.height.equalTo(30)
        }
        
        
        
        
        self.testJson()
        self.stringToJson()
        
        // Do any additional setup after loading the view.
    }
    
    
    func printDataClick(_ btn:UIButton){
        
        print("btn 点击事件")
        
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    //MARK:----------
    //MARK:json解析
    //第三可以用SwiftJSON
    func testJson(){
        //swift对象
        let user:[String:Any] = [
            "uname": "张三",
            "tel": ["mobile": "138", "home": "101"]
        ]
        
        //首先判断能不能转换
        if (!JSONSerialization.isValidJSONObject(user)){
            print("is not a valid json object")
            return
        }
        
        //利用自带的json库转换成Data
        //如果设置options为JSONSerialization.WritingOptions.prettyPrinted,则打印格式更好阅读
        let data = try? JSONSerialization.data(withJSONObject: user, options: [])
        //Data转换成String打印输出
        let str = String(data: data!, encoding: String.Encoding.utf8)
        //输出json字符串
        print("Json str:\n \(String(describing: str))")
        
        print("\n*********************")
        
        //把Data对象转换回JSON对象
        var json = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:Any]
        print("Json Object: \n \(String(describing: json))")
        print("Json Object:", json ?? "数据为空")
        
        print("\n***********************")
        
        //验证JSON对象可用性
        let uname = json?["uname"] as! String
        let mobile = (json?["tel"] as! [String:Any])["mobile"] as! String
        print("get Json Object: nuame:\(String(describing: uname)), mobile:\(String(describing: mobile))");
        
        print("\n#############结束一个，开始下一个############")
        
    }
    
    //解析json字符串
    func stringToJson(){
        
        let jsonString = "[{\"ID\":1,\"Name\":\"元台禅寺\",\"LineID\":1},{\"ID\":2,\"Name\":\"田坞里山塘\",\"LineID\":1},{\"ID\":3,\"Name\":\"滴水石\",\"LineID\":1}]"
        let data = jsonString.data(using: String.Encoding.utf8)
        
        let jsonArr = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String:Any]]
        
        print("记录数：\(jsonArr.count)")
        for json in jsonArr {
            print("ID:", json["ID"]!, "  Name:", json["Name"]!)
        }
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
